package com.zhidisoft.Taxer.springmvccontroller;

import com.zhidisoft.manage.dao.TaxOrganDao;
import com.zhidisoft.system.dao.TaxerDao;
import com.zhidisoft.system.entity.Taxer;
import com.zhidisoft.system.entity.User;
import com.zhidisoft.util.FormatDate;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class TaxerSpringMvc {

    /**
     * 跳转办税专员页面
     *
     * @return
     */
    @RequestMapping("/toListTaxer.do")
    public String toListTaxer() {
        return "listTaxer";
    }

    /**
     * 获取办税专员信息
     *
     * @param page
     * @param rows
     * @param taxerName
     * @return
     */
    @RequestMapping("/listTaxer.do")
    @ResponseBody
    public ModelMap listTaxer(String page, String rows, String taxerName) {
        ModelMap modelMap = new ModelMap();
        int pageNum = 1;
        if (page != null && !page.isEmpty()) {
            pageNum = Integer.parseInt(page);
        }
        int pageSize = 10;
        if (rows != null && !rows.isEmpty()) {
            pageSize = Integer.parseInt(rows);
        }
        List<Map<String, String>> result = TaxerDao.getSearchResult(pageNum, pageSize, taxerName);
        JSONArray jsonArray = new JSONArray();
        for (Map map : result) {
            jsonArray.add(map);
        }
        int totalRows = TaxerDao.getSearchRows(taxerName);
        modelMap.put("total", totalRows);
        modelMap.put("rows", jsonArray);
        return modelMap;
    }

    /**
     * 展示办税专员详情
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/getTaxerInfo.do")
    public String getTaxerInfo(Model model, String id) {
        Taxer taxer = TaxerDao.getTaxerById(Integer.parseInt(id));
        model.addAttribute("taxer", taxer);
        model.addAttribute("organ", TaxOrganDao.getOrgan(taxer.getOrganId()));
        return "taxerInfo";
    }

    /**
     * 删除办税专员
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteTaxer.do")
    @ResponseBody
    public ModelMap deleteTaxer(String id) {
        ModelMap modelMap = new ModelMap();
        boolean b = TaxerDao.deleteTaxer(id);
        if (b) {
            modelMap.put("success", true);
            modelMap.put("msg", "删除成功");
        } else {
            modelMap.put("success", false);
            modelMap.put("msg", "删除失败");
        }
        return modelMap;
    }

    /**
     * 携带数据到editTaxer
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toEditTaxer.do")
    public String toEditTaxer(String id, Model model) {
        Taxer taxer = TaxerDao.getTaxerById(Integer.parseInt(id));
        model.addAttribute("taxer", taxer);
        model.addAttribute("organs", TaxOrganDao.getOrgans());
        return "editTaxer";
    }

    /**
     * 修改editTaxer信息
     *
     * @param id
     * @param taxerCode
     * @param taxerName
     * @param mobile
     * @param address
     * @param sex
     * @param birthday
     * @param email
     * @param organId
     * @param session
     * @return
     */
    @RequestMapping("/editTaxer.do")
    @ResponseBody
    public ModelMap editTaxer(String id, String taxerCode, String taxerName, String mobile, String address, String sex, String birthday,
                              String email, String organId, HttpSession session) {
        ModelMap modelMap = new ModelMap();
        // 建立对象，获取和设置参数
        Taxer taxer = new Taxer();

        taxer.setId(Integer.parseInt(id));

        taxer.setTaxerCode(taxerCode);

        taxer.setTaxerName(taxerName);

        taxer.setMobile(mobile);

        taxer.setAddress(address);

        taxer.setSex(sex);

        if (StringUtils.isNotEmpty(birthday))
            taxer.setBirthday(birthday);

        taxer.setEmail(email);

        if ("-1".equals(organId)) {
            taxer.setOrganId(null);
        } else {
            taxer.setOrganId(Integer.parseInt(organId));
        }

        Date date = new Date();
        taxer.setRecordDate(FormatDate.format(date));
        User user = (User) session.getAttribute("user");
        taxer.setRecordUserId(user.getId());
        // 保存
        boolean b = TaxerDao.updateTaxer(taxer, Integer.parseInt(id));
        if (b) {
            modelMap.put("success", true);
            modelMap.put("msg", "保存成功");
        } else {
            modelMap.put("success", false);
            modelMap.put("msg", "保存失败");
        }
        return modelMap;
    }

    /**
     * 跳转添加办税专员页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/toAddTaxer.do")
    public String toAddTaxer(Model model) {
        model.addAttribute("organs", TaxOrganDao.getOrgans());
        return "addTaxer";
    }

    /**
     * 添加办税专员信息
     *
     * @param session
     * @param taxerCode
     * @param taxerName
     * @param mobile
     * @param address
     * @param sex
     * @param birthday
     * @param email
     * @param organId
     * @return
     */
    @RequestMapping("/addTaxer.do")
    @ResponseBody
    public ModelMap addTaxer(HttpSession session, String taxerCode, String taxerName, String mobile, String address, String sex, String birthday,
                             String email, String organId) {
        // 建立对象，获取和设置参数，对于和外键相连的字段判断是否有选择
        Taxer taxer = new Taxer();

        taxer.setTaxerCode(taxerCode);

        taxer.setTaxerName(taxerName);

        taxer.setMobile(mobile);

        taxer.setAddress(address);

        taxer.setSex(sex);

        if (StringUtils.isNotEmpty(birthday))
            taxer.setBirthday(birthday);

        taxer.setEmail(email);

        if ("-1".equals(organId)) {
            taxer.setOrganId(null);
        } else {
            taxer.setOrganId(Integer.parseInt(organId));
        }

        Date date = new Date();
        taxer.setRecordDate(FormatDate.format(date));
        User user = (User) session.getAttribute("user");
        taxer.setRecordUserId(user.getId());
        // 保存
        boolean b = TaxerDao.addTaxer(taxer);
        ModelMap modelMap = new ModelMap();
        if (b) {
            modelMap.put("success", true);
            modelMap.put("msg", "添加成功");
        } else {
            modelMap.put("success", false);
            modelMap.put("msg", "添加失败");
        }

        return modelMap;

    }
}
